package com.jule.robot.strategy.impl;

import JoloProtobuf.GameSvr.JoloGame;
import com.jule.robot.service.holder.GiftsHolder;
import com.jule.robot.service.websocket.BaseWebSocketClient;
import com.jule.robot.strategy.BaseRobotStrategry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class GiftsListStrategry extends BaseRobotStrategry {
    private final static Logger logger = LoggerFactory.getLogger(GiftsListStrategry.class);
    public GiftsListStrategry() {
    }

    @Override
    public void doAction(BaseWebSocketClient client, int functionId, byte[] bytes) {
//        setClient(client);
        try {
            String key = client.getGameId()+"_"+client.getRoomId();
            JoloGame.JoloGame_GiftsListAck ack = JoloGame.JoloGame_GiftsListAck.parseFrom(bytes);
            logger.debug("GiftsListStrategry, result->{}, key->{}, list->{}, roomId->{}", ack.getResult(), key, ack.getItemInfoListList().size(), ack.getRoomId());

            if(ack.getResult() == 1){
                List<JoloGame.JoloGame_ItemInfo> itemList = new ArrayList<>();
                for(JoloGame.JoloGame_ItemInfo itemInfo : ack.getItemInfoListList()){
                    if(itemInfo.getItemId().equals("17")
                            || itemInfo.getItemId().equals("18")
                            || itemInfo.getItemId().equals("19")
                            || itemInfo.getItemId().equals("20")){
                        itemList.add(itemInfo);
                    }
                }

                if(!GiftsHolder.giftsMap.containsKey(key)){
                    GiftsHolder.giftsMap.put(key, itemList);
                }
            }
        }catch (Exception ex){
            logger.error("AckGiftsList error, msg = "+ex.getMessage(), ex);
        }
    }
}
